package com.wmh.baseservice.operationaid.controller.product;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wmh.baseservice.common.mybatisplus.enums.SortMode;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.web.UserUtil;
import com.wmh.baseservice.operationaid.entity.ProductMonitorRecord;
import com.wmh.baseservice.operationaid.pojo.vo.product.monitorrecored.ProductMonitorRecordCommitReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.product.monitorrecored.ProductMonitorRecordPageReqVO;
import com.wmh.baseservice.operationaid.service.ProductMonitorRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.wmh.baseservice.common.utils.returnbody.ReturnBody.success;

/**
 * @author 谢骏
 * @date 2023/9/26 14:03
 **/
@Validated
@RestController
@RequestMapping("operationAid/product/monitor-record")
@Api(tags = "运营助手-商品-监控单元-记录")
public class ProductMonitorRecordController {


    @Resource
    private ProductMonitorRecordService productMonitorRecordService;

    @Resource
    private UserUtil userUtil;

    @ApiModelProperty("机器提交监控数据")
    @PostMapping("public/robot-commit")
    public String robotCommit(@RequestBody @Valid ProductMonitorRecordCommitReqVO reqVO) {
        productMonitorRecordService.robotCommit(reqVO);
        return success();
    }

    @ApiModelProperty("分页")
    @GetMapping("page")
    public String page(@Valid ProductMonitorRecordPageReqVO reqVO) {
        PageResult<ProductMonitorRecord> pageResult = new PageUtil<ProductMonitorRecord>(reqVO).startPage((page, wrapper) -> {
            LambdaQueryWrapper<ProductMonitorRecord> l = wrapper.lambda();
            l.eq(reqVO.getProductId() != null, ProductMonitorRecord::getProductId, reqVO.getProductId());
            l.eq(reqVO.getPushUnitId() != null, ProductMonitorRecord::getProductId, reqVO.getPushUnitId());
            l.eq(reqVO.getSkuId() != null, ProductMonitorRecord::getSkuId, reqVO.getSkuId());
            l.like(StrUtil.isNotBlank(reqVO.getSkuContent()), ProductMonitorRecord::getSkuContent, reqVO.getSkuContent());
            if (userUtil.isSuperAdmin()) {
                if (reqVO.getAdId() != null) {
                    l.eq(ProductMonitorRecord::getAdId, reqVO.getAdId());
                } else {
                    l.eq(reqVO.getLookMe() != null && reqVO.getLookMe(), ProductMonitorRecord::getAdId, userUtil.getId());
                }

            } else {
                l.eq(ProductMonitorRecord::getAdId, reqVO.getAdId());
            }
            if (StrUtil.isNotBlank(reqVO.getSortField())) {
                if (SortMode.DESC.equals(reqVO.getSortMode())) {
                    wrapper.orderByDesc(StrUtil.toUnderlineCase(reqVO.getSortField()));
                } else {
                    wrapper.orderByAsc(StrUtil.toUnderlineCase(reqVO.getSortField()));
                }

            } else {
                l.orderByDesc(ProductMonitorRecord::getCreateTime);
            }
            productMonitorRecordService.page(page, wrapper);
        });
        return success(pageResult);
    }


}
